package com.itheima.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.attendance.mapper.AttenArchiveMonthInfoMapper;
import com.itheima.attendance.mapper.AttenArchiveMonthMapper;
import com.itheima.attendance.mapper.AttendanceMapper;
import com.itheima.attendance.mapper.UserMapper;
import com.itheima.attendance.pojo.ArchiveMonthly;
import com.itheima.attendance.pojo.ArchiveMonthlyInfo;
import com.itheima.attendance.pojo.User;
import com.itheima.attendance.service.AttenArchiveService;
import com.itheima.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/13 10:16
 */
@Service
@Transactional
@SuppressWarnings(value = "all")
public class AttenArchiveServiceImpl extends ServiceImpl<AttenArchiveMonthInfoMapper,ArchiveMonthlyInfo> implements AttenArchiveService {

    @Autowired
    private AttenArchiveMonthMapper attenArchiveMonthMapper;

    @Autowired
    private AttenArchiveMonthInfoMapper attenArchiveMonthInfoMapper;

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询月考勤归档数据
     */
    @Override
    public List<ArchiveMonthlyInfo> findAttenDateByDateMonth(String companyId, String archiveDate) {
        //最终返回的月考勤归档数据
        List<ArchiveMonthlyInfo> archiveMonthlyInfoList=new ArrayList<ArchiveMonthlyInfo>(0);
        //根据企业id查询企业用户
        List<User> userList =this.findUserListByCompanyId(companyId);
        //遍历userList
        for (User user : userList) {
            ArchiveMonthlyInfo info=new ArchiveMonthlyInfo(user);
            //根据userId和archiveDate查询当前用户当月的考勤数据情况
            info.setStatisData(attendanceMapper.findAttenInfoByUserIdAndArchiveDate(user.getId(),archiveDate));
            archiveMonthlyInfoList.add(info);
        }
        if(CollectionUtils.isEmpty(archiveMonthlyInfoList)){
            return null;
        }
        return archiveMonthlyInfoList;
    }

    /**
     * 月考勤数据归档保存
     */
    @Override
    public void saveArchive(String archiveDate, String companyId, String userId) {
        //根据企业id查询企业用户
        List<User> userList = this.findUserListByCompanyId(companyId);
        ArchiveMonthly archiveMonthly=new ArchiveMonthly();
        archiveMonthly.setId(String.valueOf(idWorker.nextId()));
        archiveMonthly.setCompanyId(companyId);
        archiveMonthly.setDepartmentId(this.findDepartmentIdByUserIdAndCompanyId(companyId,userId));
        archiveMonthly.setArchiveYear(archiveDate.substring(0,4));
        archiveMonthly.setArchiveMonth(archiveDate.substring(5));
        archiveMonthly.setTotalPeopleNum(userList.size());
        archiveMonthly.setIsArchived(0);
        //全勤人数
        archiveMonthly.setFullAttePeopleNum(this.findfullAttePeopleNumByCompanyId(companyId,archiveDate));
        archiveMonthly.setCreateBy(userId);
        archiveMonthly.setCreateDate(new Date());
        //月考勤归档保存  ----总
        attenArchiveMonthMapper.insert(archiveMonthly);
        //归档明细    ---- 明细
        List<ArchiveMonthlyInfo> archiveMonthlyInfoList = this.findAttenDateByDateMonth(companyId, archiveDate);
        //TODO... 还有很多要就算的数据，后期补上，不如应出勤多少天，实际出勤多少天等等。。。。。。
        archiveMonthlyInfoList.forEach(archiveMonthlyInfo -> {
                    archiveMonthlyInfo.setId(String.valueOf(idWorker.nextId()));
                    archiveMonthlyInfo.setAtteArchiveMonthlyId(archiveMonthly.getId());
                    archiveMonthlyInfo.setArchiveDate(archiveDate);});
        //月度考勤数据批量入库保存
       saveBatch(archiveMonthlyInfoList);
    }

    /**
     * 根据企业id查询全勤人数
     */
    private Integer findfullAttePeopleNumByCompanyId(String companyId,String archiveDate){
       return attendanceMapper.findfullAttePeopleNumByCompanyId(companyId,archiveDate);
    }

    /**
     * 月归档查询归档人的部门id
     */
    private String findDepartmentIdByUserIdAndCompanyId(String companyId,String userId){
        QueryWrapper<User> userQuery=new QueryWrapper<User>();
        userQuery.and(i->i.eq("company_id",companyId).eq("id",userId));
        User user = userMapper.selectOne(userQuery);
        if(user==null){
            return null;
        }
        return user.getDepartmentId();
    }

    /**
     * 根据企业id查询企业用户
     */
    private List<User> findUserListByCompanyId(String companyId) {
        QueryWrapper<User> userQuery=new QueryWrapper<User>();
        userQuery.eq("company_id",companyId);
        List<User> userList = userMapper.selectList(userQuery);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        }
        return userList;
    }

    /**
     * 根据年份和企业id查询年度考勤
     */
    @Override
    public List<ArchiveMonthly> findArchiveMonthlyByYearAndCompanyId(String year, String companyId) {
        QueryWrapper<ArchiveMonthly> archiveMonthlyQuery=new QueryWrapper<ArchiveMonthly>();
        archiveMonthlyQuery.and(i->i.eq("archive_year",year).eq("company_id",companyId));
        List<ArchiveMonthly> archiveMonthlyList = attenArchiveMonthMapper.selectList(archiveMonthlyQuery);
        if(CollectionUtils.isEmpty(archiveMonthlyList)){
            return null;
        }
        return archiveMonthlyList;
    }

    /**
     * 根据年度考勤的归档id查询考勤归档明细
     */
    @Override
    public List<ArchiveMonthlyInfo> findArchiveMonthInfoByArchiveId(String id) {
        QueryWrapper<ArchiveMonthlyInfo> archiveMonthlyInfoQuery=new QueryWrapper<ArchiveMonthlyInfo>();
        archiveMonthlyInfoQuery.eq("atte_archive_monthly_id",id);
        List<ArchiveMonthlyInfo> archiveMonthlyInfoList = attenArchiveMonthInfoMapper.selectList(archiveMonthlyInfoQuery);
        if (CollectionUtils.isEmpty(archiveMonthlyInfoList)) {
            return null;
        }
        return archiveMonthlyInfoList;
    }

    @Override
    public ArchiveMonthlyInfo findArichiveInfoByUserIdAndYearMonth(String userId, String yearMonth) {
        QueryWrapper<ArchiveMonthlyInfo> archiveMonthlyInfoQuery=new QueryWrapper<ArchiveMonthlyInfo>();
        archiveMonthlyInfoQuery.and(i->i.eq("user_id",userId).eq("archive_date",yearMonth));
        ArchiveMonthlyInfo info = attenArchiveMonthInfoMapper.selectOne(archiveMonthlyInfoQuery);
        if(info==null){
            return null;
        }
        return info;
    }
}
